//
//  SellCollection.m
//  LoveCar
//
//  Created by Xue Senhong on 15/6/2.
//  Copyright (c) 2015年 薛森鸿. All rights reserved.
//

#import "SellCollection.h"
#import "SellSortViewController.h"



@implementation SellCollection

- (void)dealloc
{
    [_listArray release];
    [_sortTableView release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.listArray = [NSMutableArray array];
        [self createTableView];
    }
    return self;
}



- (void)createTableView
{
    UITableView *sortTableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, kScreenHeight * 50 / 667, self.frame.size.width, self.frame.size.height - 25 - 64) style:UITableViewStylePlain] autorelease];
    sortTableView.delegate = self;
    sortTableView.dataSource = self;
    [sortTableView registerClass:[SellSortCell class] forCellReuseIdentifier:@"cell"];
    [self addSubview:sortTableView];
    [sortTableView release];
}
#pragma tableView的协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kScreenHeight * 110 / 667;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SellSortCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    SellSortModel *model = [self.listArray objectAtIndex:indexPath.row];
    if ([model.WhiteCoverImg containsString:@"{0}"]) {
        NSRange range = [model.WhiteCoverImg rangeOfString:@"{0}"];
        NSMutableString *str = [model.WhiteCoverImg mutableCopy];
        [str replaceCharactersInRange:range withString:@"3"];
        model.WhiteCoverImg = str;
    }
    [cell.picImage sd_setImageWithURL:[NSURL URLWithString:model.WhiteCoverImg] placeholderImage:nil];
    cell.rankLabel.text = [NSString stringWithFormat:@"%ld", (long)model.Rank];
    if (indexPath.row < 3) {
        [cell.rankLabel setBackgroundColor:[UIColor colorWithRed:1 green:0.3 blue:0.25 alpha:1]];
    } else {
        [cell.rankLabel setBackgroundColor:[UIColor colorWithRed:0.3 green:0.7 blue:1 alpha:1]];
    }
    cell.nameLabel.text = model.SerialName;
    cell.priceLabel.text = [NSString stringWithFormat:@"%.2f万-%.2f万", model.MinPrice, model.MaxPrice];
    cell.sellLabel.text = [NSString stringWithFormat:@"全国销量: %ld辆", (long)model.Index];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate pushToDetail:indexPath];
}


@end
